package com.atguigu.tingshu.order.api;

import com.atguigu.tingshu.common.login.RequiresLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "订单管理")
@RestController
@RequestMapping("api/order")
public class OrderInfoApiController {

    private final OrderInfoService orderInfoService;

    public OrderInfoApiController(OrderInfoService orderInfoService) {
        this.orderInfoService = orderInfoService;
    }


    @Operation(summary = "订单结算页面数据汇总（VIP会员、专辑、声音）")
    @Parameters({
            @Parameter(name = "tradeVo", description = "订单确认对象", required = true)
    })
    @RequiresLogin
    @PostMapping("/orderInfo/trade")
    public Result<OrderInfoVo> trade(@RequestBody @Validated TradeVo tradeVo) {
        Long userId = AuthContextHolder.getUserId();
        OrderInfoVo orderInfoVo = this.orderInfoService.trade(userId, tradeVo);
        return Result.ok(orderInfoVo);
    }

    @Operation(summary = "提交订单")
    @Parameters({
            @Parameter(name = "orderInfoVo", description = "订单提交对象", required = true)
    })
    @RequiresLogin
    @PostMapping("/orderInfo/submitOrder")
    public Result<Map<String, String>> submitOrder(@RequestBody @Validated OrderInfoVo orderInfoVo) {
        Long userId = AuthContextHolder.getUserId();
        Map<String, String> result = this.orderInfoService.submitOrder(userId, orderInfoVo);
        return Result.ok(result);
    }

    @Operation(summary = "根据订单号获取订单相关信息")
    @Parameters({
            @Parameter(name = "orderNo", description = "订单号", required = true)
    })
    @RequiresLogin
    @GetMapping("/orderInfo/getOrderInfo/{orderNo}")
    public Result<OrderInfo> getOrderInfo(@PathVariable String orderNo) {
        Long userId = AuthContextHolder.getUserId();
        OrderInfo orderInfo = this.orderInfoService.getOrderInfoByOrderNo(userId, orderNo);
        return Result.ok(orderInfo);
    }

    @Operation(summary = "分页获取当前用户订单列表")
    @Parameters({
            @Parameter(name = "page", description = "当前页码", required = true),
            @Parameter(name = "limit", description = "每页记录数", required = true)
    })
    @RequiresLogin
    @GetMapping("/orderInfo/findUserPage/{page}/{limit}")
    public Result<Page<OrderInfo>> getUserOrderByPage(@PathVariable int page,
                                                      @PathVariable int limit) {
        Long userId = AuthContextHolder.getUserId();
        Page<OrderInfo> pageInfo = new Page<>(page, limit);
        pageInfo = orderInfoService.getUserOrderByPage(pageInfo, userId);
        return Result.ok(pageInfo);
    }


    @Operation(summary = "在支付成功后更新订单")
    @Parameters({
            @Parameter(name = "orderNo", description = "订单号", required = true)
    })
    @RequiresLogin
    @GetMapping("/orderInfo/updateOrderOnPaySuccess/{orderNo}")
    public Result<Void> updateOrderOnPaySuccess(@PathVariable String orderNo) {
        this.orderInfoService.updateOrderOnPaySuccess(orderNo);
        return Result.ok();
    }


}

